package main;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;

import util.Options;
import util.Service;
import util.TextIO;
import weka.classifiers.bayes.NaiveBayes;
import weka.clusterers.forOPTICSAndDBScan.DataObjects.DataObject;
import classifier.ClassifierBuilder;
import classifier.IClassifier;
import classifier.Item;
import classifier.WeightedMajority;
import classifier.WekaClassifier;

public class mytest {

	/**
	 * @param args
	 * @throws Exception
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException,
			IOException, Exception {
//		String[] t={""};
//		args=t;
//		args[0]=("runserver");
//		
		if (args[0].equals("train")) {
			ClassifierBuilder clb = new ClassifierBuilder();
			Options opt = new Options();
			clb.setOpt(opt);

			opt.setSelectedFeaturesByFrequency(true);

			opt.setNumFeatures(150);

			opt.setRemoveEmoticons(true);

			clb.prepareTrain();
			clb.prepareTest();

			NaiveBayes nb = new NaiveBayes();

			WekaClassifier wc = clb.constructClassifier(nb);

			TextIO.readFile("sample.txt");
			String line = "";
			while (!TextIO.eof()) {
				line = TextIO.getln();
				String result = wc.classify(line);
				TextIO.put(line + "------is -----");
				if (result.equals("4")) {
					TextIO.putln("Positive ");
				}
				if (result.equals("0")) {
					TextIO.putln("Negative ");

				}
			}
			line = "Your song is not annoying.";
			String result = wc.classify(line);

			TextIO.put(line + "------is -----");
			if (result.equals("4")) {
				TextIO.putln("Positive ");
			}
			if (result.equals("0")) {
				TextIO.putln("Negative ");

			}
			try {
				FileOutputStream fileOut = new FileOutputStream("nbc.ser");
				ObjectOutputStream out = new ObjectOutputStream(fileOut);
				out.writeObject(wc);
				out.close();
				fileOut.close();
			} catch (IOException i) {
				i.printStackTrace();
			}

		}
		if (args[0].equals("test")) {

			WekaClassifier wc = null;
			try {
				FileInputStream fileIn = new FileInputStream("nbc.ser");
				ObjectInputStream in = new ObjectInputStream(fileIn);
				wc = (WekaClassifier) in.readObject();
				in.close();
				fileIn.close();
			} catch (IOException i) {
				i.printStackTrace();
				return;
			} catch (ClassNotFoundException c) {
				System.out.println("WekaClassifier class not found");
				c.printStackTrace();
				return;
			}

			if (args[1] == null) {
				System.err.println("NO INPUT");

				return;
			}
			if (args[1].equals("")) {
				System.err.println("NO INPUT");

				return;
			}

			Gson gson = new Gson();

			try {

				BufferedReader br = new BufferedReader(new FileReader(args[1]));

				// convert the json string back to object
				DataObject obj = gson.fromJson(br, DataObject.class);

				System.out.println(obj);

			} catch (IOException e) {
				e.printStackTrace();
			}

			String result = wc.classify(args[1]);
			if (result.equals("4")) {
				TextIO.putln("Positive ");
			}
			if (result.equals("0")) {
				TextIO.putln("Negative ");

			}

		}
		if (args[0].equals("runserver")) {
			
			
			runserver();
			
		}
	}
	
	public static void runserver(){
	
	Service se=new Service(4567);
		se.start();
	}

}
